<?php
class BrandModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->check = new BrandCheck();
        $this->tables = array(DB_FREFIX.'brand');
        $this->fields = array('id','name','url','info','reg_time');
        list(
            $this->R['id'],
            $this->R['name']
        ) = $this->getRequest()->getParams(
            array(
               isset($_GET['id']) ? $_GET['id'] : null,
               isset($_POST['name']) ? $_POST['name'] : null
            )
        );
    }

    public function findOne() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        return parent::select($this->tables,array('id','name','url','info'),array('where'=>$condition,'limit'=>1));
    }

    public function findNavBrand() {
        return parent::select($this->tables,array('id','name'));
    }

    public function findAll() {
        return parent::select($this->tables,array('id','name','url','info'),array('limit'=>$this->limit));
    }

    public function add() {
        $condition = array('name='.$this->R['name']);
        if(!$this->check->addCheck($this,$condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        $needData['reg_time'] = Tool::getDate();
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        if(!$this->check->updateCheck()) $this->check->displayError();
        $updateData = $this->getRequest()->filter($this->fields);
        return parent::update($updateData, $condition);
    }

    public function delete() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        return parent::delete($condition);
    }

    public function ajaxCheckName() {
        $condition = array('name='.$this->R['name']);
        $this->check->ajaxCheckName($this,$condition);
    }

    //ajax获取品牌类型，使用的格式为 id:value
    public function getBrand() {
        $nav = parent::select(array(DB_FREFIX.'nav'),array('brand'),array('where'=>array('id='.$this->R['id'])));
        if($nav[0]->brand == '') {
            echo '0:其他品牌';
            return;
        }
        $brand = implode(',',unserialize(htmlspecialchars_decode($nav[0]->brand)));
        $brandArr = parent::select(array(DB_FREFIX.'brand'),array('id','name'),array('where'=>array('id in('.$brand.')')));
        $brandStr = '';
        foreach($brandArr as $key=>$value) {
            $brandStr .= $value->id.':'.$value->name.':';
        }
        $brandStr = substr($brandStr,0,-1);
        echo $brandStr;
    }

    public function total() {
        return parent::total();
    }

}




?>